<template>
  <div style="margin-left: 20px;">
    <div class="container">
      <el-tabs v-model="activeTabName" @tab-click="handleClick" class="tab">
        <el-tab-pane label="全部" name="all" />
        <el-tab-pane label="在线设备" name="online" />
        <el-tab-pane label="离线设备" name="offline" />
      </el-tabs>
      <div style="flex: 1;" />

      <el-date-picker v-model="value1" type="daterange" range-separator="至" start-placeholder="开始日期"
        end-placeholder="结束日期" class="flex-item"></el-date-picker>
      <el-input placeholder="请输入内容" v-model="searchKeyword" @input="handleSearch" style="width: 250px;">
        <el-button slot="append" icon="el-icon-search" @click="handleSearch" class="sysbtn">查询</el-button>
      </el-input>
      <el-button size="small" class="flex-item batchDelete" @click="handleBatchDelete">批量删除</el-button>

    </div>
    <!-- 表格 -->
    <el-table ref="multipleTable" :data="currentTableData" style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="deviceName" label="设备名称" width="200" show-overflow-tooltip sortable></el-table-column>
      <el-table-column prop="serialNumber" label="设备序列号" width="300" show-overflow-tooltip sortable></el-table-column>
      <el-table-column prop="phoneNumber" label="手机号码" width="300" show-overflow-tooltip sortable></el-table-column>

      <el-table-column prop="isOnline" label="在线状态" show-overflow-tooltip sortable>
        <template slot-scope="scope">
          {{ scope.row.isOnline ? '在线' : '不在线' }}
        </template>
      </el-table-column> <el-table-column prop="firstAccessTime" label="首次接入时间" width="200"></el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="text" @click="handleDetail(scope.row)">详情</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentPageChange" :current-page="currentPage"
      :page-size="pageSize" :page-sizes="[10, 20, 50, 100]" layout="total, sizes, prev, pager, next, jumper"
      :total="totalDataCount" class="pagination">
    </el-pagination>



    <!-- <el-dialog title="设备详情" :visible.sync="detailDialogVisible">
      <div>
        <p><strong>设备名称：</strong>{{ detailRow.deviceName }}</p>
        <p><strong>设备序列号：</strong>{{ detailRow.serialNumber }}</p>
        <p><strong>手机号码：</strong>{{ detailRow.phoneNumber }}</p>
        <p><strong>在线状态：</strong>{{ detailRow.onlineStatus }}</p>
        <p><strong>首次接入时间：</strong>{{ detailRow.firstAccessTime }}</p>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="detailDialogVisible = false">关闭</el-button>
      </span>
    </el-dialog> -->
    <el-dialog title="终端设备详情" :visible.sync="detailDialogVisible" width="50%">
      <div class="apk-detail-content">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>基础信息</span>
          </div>
          <el-row :gutter="20" class="form">
            <el-col :span="6">
              <div class="grid-content bg-purple">
                设备序列号：4565615
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                设备品牌型号：华为mate60
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                操作系统：HarmonyOS 4.2.0.128
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                用户admin
              </div>
            </el-col>
          </el-row>
          <el-row :gutter="20" class="form">
            <el-col :span="6">
              <div class="grid-content bg-purple">
                手机号码：13000001234
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                状态：在线
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                安装apk数量：29
              </div>
            </el-col>
            <el-col :span="6">
              <div class="grid-content bg-purple">
                风险apk数量：12
              </div>
            </el-col>
          </el-row>
        </el-card>
        <!-- 下面部分 -->
        <div>
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <div style="margin-bottom: 10px;">
                APP列表
              </div>

              <el-table :data="tableData2" border style="width: 100%">
                <el-table-column prop="name" label="APP名称" width="180" />
                <el-table-column prop="version" label="版本" width="120" />
                <el-table-column prop="releaseTime" label="发布时间" width="180" />
                <el-table-column prop="company" label="所属公司" width="180" />
                <el-table-column prop="size" label="大小" width="120" />
                <el-table-column prop="riskStatus" label="风险情况" width="120" />
              </el-table>
            </div>
          </el-card>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="detailDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchKeyword: '',
      currentPage: 1,
      value1: '',
      activeTabName: 'all',
      pageSize: 6,
      totalDataCount: 0,
      tableData: [],
      filteredTableData: [],
      selectedRows: [],
      editForm: {},
      detailDialogVisible: false,
      detailRow: {},
       // 查询参数
       queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeTitle: undefined,
        createBy: undefined,
        status: undefined
      },
      tableData2: [
        {
          name: "抖音",
          version: "1.2.3",
          releaseTime: "2023-04-01",
          company: "北京微播世界科技有限公司",
          size: "131MB",
          riskStatus: "访问和收集个人信息",
        },
        {
          name: "爱奇艺",
          version: "2.2.3",
          releaseTime: "2023-04-01",
          company: "北京微播世界科技有限公司",
          size: "131MB",
          riskStatus: "收集通讯录信息",
        },
        {
          name: "腾讯视频",
          version: "2.12.3",
          releaseTime: "2023-04-01",
          company: "北京微播世界科技有限公司",
          size: "131MB",
          riskStatus: "查看相册",
        },
        {
          name: "酷狗音乐",
          version: "2.2.4",
          releaseTime: "2023-04-01",
          company: "北京微播世界科技有限公司",
          size: "131MB",
          riskStatus: "植入病毒",
        },
        {
          name: "QQ音乐",
          version: "3.12.1",
          releaseTime: "2023-04-01",
          company: "北京微播世界科技有限公司",
          size: "131MB",
          riskStatus: "恶意吸费",
        },
      ],
    };
  },
  computed: {
    currentTableData() {
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      return this.filteredTableData.slice(start, end);
    },
  },
  methods: {
    handleSearch() {
      this.filterData();
      this.currentPage = 1;
    },
    filterData() {
      this.filteredTableData = this.tableData.filter(item => {
        return Object.keys(item).some(key =>
          String(item[key]).includes(this.searchKeyword)
        );
      });
      this.totalDataCount = this.filteredTableData.length;
    },
    handleSizeChange(newSize) {
      this.pageSize = newSize;
      this.filterData();
    },
    handleCurrentPageChange(newPage) {
      this.currentPage = newPage;
    },
    handleSelectionChange(rows) {
      this.selectedRows = rows;
    },
    handleDetail(row) {
      this.detailRow = { ...row };
      this.detailDialogVisible = true;
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该设备, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        const index = this.tableData.indexOf(row);
        if (index !== -1) {
          this.tableData.splice(index, 1);
        }
        this.$message({
          type: 'success',
          message: '删除成功!',
        });
        this.filterData();
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除',
        });
      });
    },
    handleBatchDelete() {
      if (this.selectedRows.length === 0) {
        this.$message({
          message: '请至少选择一个设备进行删除',
          type: 'warning'
        });
        return;
      }
      this.$confirm('确认删除这些任务吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.tableData = this.tableData.filter(item => {
          return !this.selectedRows.some(selectedItem => selectedItem.deviceName === item.deviceName);
        });
        this.$refs.multipleTable.clearSelection();
        this.selectedRows = [];

        this.$message({
          message: '批量删除成功',
          type: 'success'
        });
        this.filterData();
      }).catch(() => {
        this.$message({
          message: '已取消批量删除',
          type: 'info'
        });
      });
    },


    handleClick(tab, event) {
      const tabName = tab.name;
      switch (tabName) {
        case 'online':
          this.filteredTableData = this.tableData.filter(item => item.isOnline);
          break;
        case 'offline':
          this.filteredTableData = this.tableData.filter(item => !item.isOnline);
          break;
        default:
          this.filteredTableData = [...this.tableData];
          break;
      }
      this.totalDataCount = this.filteredTableData.length;
      this.currentPage = 1;
    },
    clearEditData() {
      this.editForm = {
        deviceName: '',
        serialNumber: '',
        phoneNumber: '',
        onlineStatus: '',
        firstAccessTime: '',
      };
    },
  },
  created() {
    this.tableData = [
      {
        deviceName: '设备1',
        serialNumber: 'ocr-license-2542',
        phoneNumber: '14566845000',
        isOnline: true,
        firstAccessTime: '2022-01-01 10:00:00',
      },
      {
        deviceName: '设备2',
        serialNumber: 'ocr-license-54245',
        phoneNumber: '14157515000',
        isOnline: true, firstAccessTime: '2022-01-02 10:00:00',
      },
      {
        deviceName: '设备3',
        serialNumber: 'ocr-license-3456',
        phoneNumber: '13800138000',
        isOnline: true, firstAccessTime: '2022-01-03 10:00:00',
      },
      {
        deviceName: '设备4',
        serialNumber: 'ocr-license-4567',
        phoneNumber: '13456789000',
        isOnline: true, firstAccessTime: '2022-01-04 10:00:00',
      },
      {
        deviceName: '设备5',
        serialNumber: 'ocr-license-5678',
        phoneNumber: '13456789001',
        isOnline: false, firstAccessTime: '2022-01-05 10:00:00',
      },
      {
        deviceName: '设备6',
        serialNumber: 'ocr-license-6789',
        phoneNumber: '13456789002',
        onlineStatus: '在线',
        firstAccessTime: '2022-01-06 10:00:00',
      },
      {
        deviceName: '设备7',
        serialNumber: 'ocr-license-7890',
        phoneNumber: '13456789003',
        isOnline: false, firstAccessTime: '2022-01-07 10:00:00',
      },
      {
        deviceName: '设备8',
        serialNumber: 'ocr-license-8901',
        phoneNumber: '13456789004',
        isOnline: true, firstAccessTime: '2022-01-08 10:00:00',
      },
      {
        deviceName: '设备9',
        serialNumber: 'ocr-license-9012',
        phoneNumber: '13456789005',
        isOnline: false, firstAccessTime: '2022-01-09 10:00:00',
      },
      {
        deviceName: '设备10',
        serialNumber: 'ocr-license-0123',
        phoneNumber: '13456750006',
        isOnline: true, firstAccessTime: '2022-01-10 10:00:00',
      },
      {
        deviceName: '设备11',
        serialNumber: 'ocr-license-1234',
        phoneNumber: '13456750007',
        isOnline: false, firstAccessTime: '2022-01-11 10:00:00',
      },
      {
        deviceName: '设备12',
        serialNumber: 'ocr-license-2345',
        phoneNumber: '13456750008',
        isOnline: true, firstAccessTime: '2022-01-12 10:00:00',
      },
    ];
    this.filterData();
  },
};
</script>
<style scoped>
.pagination {
  display: flex;
  justify-content: center;
  margin-top: 10px;
}

.container {
  display: flex;
  align-items: center;
}
</style>